package com.example.leetdemo.finash;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution1282 {

    public List<List<Integer>> groupThePeople(int[] groupSizes) {
        List<List<Integer>> result = new ArrayList<>();
        Map<Integer, List<Integer>> m = new HashMap<>();
        for (int i = 0; i < groupSizes.length; i++) {
            List<Integer> temp = m.getOrDefault(groupSizes[i], new ArrayList<>());
            temp.add(i);
            if (temp.size() == groupSizes[i]) {
                result.add(temp);
                temp = new ArrayList<>();
            }
            m.put(groupSizes[i], temp);
        }
        return result;
    }

}
